layui.config({
    base: 'assets/layuiadmin/' //静态资源所在路径
}).extend({
    index: 'lib/index', //主入口模块
}).use(['index', 'table', 'laytpl', 'laydate'], function(){
    var $ = layui.$,
        table = layui.table,
        laydate = layui.laydate;

    laydate.render({
        elem: '#time', //指定元素
        type: 'datetime',
        range: true
    });

    table.render({
        id: 'data-table',
        elem: '#data-table',
        url: 'audit/dynamic/list',
        where: getData(),
        toolbar: '#data-table-toolbar',
        title: '动态审核列表',
        height: globalHeight,
        page: true,
        cols: [[
            {field: 'id', title: 'ID', width: 80, align: 'center'},
            {field: 'userId', title: '用户id', width: 100, align: 'center'},
            {field: 'name', title: '昵称', align: 'center'},
            {field: 'host', title: '身份', width: 70, align: 'center', templet: '#hostTpl'},
            {field: 'type', title: '类型', width: 70, align: 'center', templet: '#typeTpl'},
            {field: 'title', title: '标题', align: 'center'},
            {field: 'url', title: '动态', align: 'center', templet: '#urlTpl'},
            {field: 'coin', title: '费用', width: 75, align: 'center', templet: '#coinTpl'},
            {field: 'createTime', title: '时间', width: 160, align: 'center'},
            {field: 'status', title: '状态', width: 75, align: 'center', templet: '#statusTpl'},
            {field: 'reason', title: '理由', width: 150, align: 'center', templet:
                function (d) {
                    if(d.reason != null){
                        return d.reason.replace(/\n/gi, "<br>");
                    }
                    return "";
                }
            },
            {field: 'op', title: '操作', width: 120, align: 'center', templet: '#data-table-bar'}
        ]],
        parseData: function(result){
            return {
                "code": result.code, //解析接口状态
                "msg": result.msg, //解析提示文本
                "count": result.data.total, //解析数据长度
                "data": result.data.hostDynamics //解析数据列表
            };
        }
    });

    //头工具栏事件
    table.on('toolbar(data-table)', function(obj){
        switch(obj.event){
            case 'search':
                var data =  getData();
                table.reload('data-table', {
                    url: 'audit/dynamic/list',
                    where: data,
                    page: {
                        curr: 1
                    }
                });
                reloadData(data);
                break;
        };
    });

    //监听行工具事件
    table.on('tool(data-table)', function(obj){
        var data = obj.data;
        if(obj.event === 'pass'){
            // 加载层
            var load = layerLoad();
            post('audit/dynamic/audit', {id: data.id, status: 1}, false, function () {
                obj.update({
                    status: 1,
                    op: 1
                });
                layer.close(load);
                layer.msg("审核通过");
            });
        } else if(obj.event === 'refuse'){
            layer.open({
                type: 2,
                title: '审核拒绝',
                offset: '25%',
                content: 'toRefuseAudit',
                area: ['600px', '350px'],
                btn: ['确定', '取消'],
                success: function(layero, index){   //弹窗成功后回调
                    var iframeWindow = window['layui-layer-iframe' + index];
                    $(iframeWindow.document).find('#id').val(data.id);   //传值到iframe页面
                },
                yes: function (index, layero) {     //确定按钮回调
                    var iframeWindow = window['layui-layer-iframe'+ index],
                        submit = layero.find('iframe').contents().find("#data-submit");

                    //监听提交
                    iframeWindow.layui.form.on('submit(data-submit)', function(res){
                        var field = res.field; //获取提交的字段
                        // 加载层
                        var load = layerLoad();
                        post('audit/dynamic/audit', field, false, function (resultData) {
                            var reason = data.reason;
                            if(reason == null || reason == ''){
                                reason = field.reason;
                            }else{
                                reason += "\n"+field.reason;
                            }
                            obj.update({
                                status: 2,
                                reason: reason,
                                op: 2
                            });
                            layer.close(index); //关闭弹层
                            layer.close(load);
                            layer.msg("更新成功");
                        });
                    });

                    submit.trigger('click');
                },
            });
        } else if(obj.event === 'cancelAudit'){
            layer.prompt({
                    title: '请输入撤销理由',
                    offset: '25%',
                    area: ['100px', '50px']
                }, function(value, index, elem){
                    if(value == ''){
                        layer.msg("请输入撤销理由");
                    }else{
                        layer.close(index);
                        // 加载层
                        var load = layerLoad();
                        post('audit/dynamic/cancelAudit', {id: data.id, reason: value}, false, function () {
                            var reason = data.reason;
                            if(reason == null || reason == ''){
                                reason = value;
                            }else{
                                reason += "\n"+value;
                            }
                            obj.update({
                                status: 0,
                                reason: reason,
                                op: 2
                            });
                            layer.close(load);
                            layer.msg("撤销成功");
                        });
                    }
                }
            );
        }
    });

    /**
     * 获取查询数据
     * @returns {{host: jQuery, online: jQuery, keyword: (*|jQuery|Array|string|undefined), status: (*|jQuery|Array|string|undefined)}}
     */
    function getData() {
        var userId = $('#userId').val();
        var status = $('#status').val();
        var time = $('#time').val();

        return {'userId': userId, 'status': status, 'createTime': time};
    }

    /**
     * 重新渲染数据
     * @param data
     */
    function reloadData(data) {
        $('#userId').val(data.userId);
        $('#status').val(data.status);
        $('#time').val(data.createTime);
        laydate.render({
            elem: '#time', //指定元素
            type: 'datetime',
            range: true
        });
    }
});